// Include libraries
#include <SDL/SDL.h>

// Include game engine
#include "game_engine/game_engine.h"

// Inlucde misc
#include "color_transformations.h"
#include "gfx/tileset/tileset_specifications.h"
#include "input.h"

// Define constants
	// The width of the screen
	const u16 SCREEN_WIDTH =1024;

	// The height of the screen
	const u16 SCREEN_HEIGHT=768;

	// Bits per pixel of the screen
	const u16 SCREEN_BPP=32;

	// Frames per second
	const u16 GAME_FPS=60;

int main(int argc, char *argv[]) {

	BoundingBox bbox;

	// Initialize surface pointers
		// Null tileset surface
		SDL_Surface* nullTilesetSurface=NULL;

		// Sprites
			// Characters
				// Megaman
				SDL_Surface* srf_characterMegamanStandNormal=NULL;
				SDL_Surface* srf_megamanStandStormTornado=NULL;

				// Zero

		// Screen surface
		SDL_Surface* srf_screen=NULL;

	// Initialize SDL
	SDL_Init(SDL_INIT_VIDEO);

	// Initialize the screen
	srf_screen=SDL_SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SDL_SWSURFACE);//SDL_FULLSCREEN);

	// Load gfx
        // Sprites
            // Characters
                // Megaman
				srf_characterMegamanStandNormal=gfx_loadImage("gfx/character/megaman/stand/character_megaman_stand.png");

				// Zero


	/*
	gfx_applySurface(30, 60, srf_characterMegamanStandNormal, srf_screen);
	srf_megamanStandStormTornado=gfx_createColorTransformedSurface(srf_characterMegamanStandNormal, ctf_stormTornado, CTF_STORM_TORNADO_SIZE);
	gfx_applySurface(60, 90, srf_megamanStandStormTornado, srf_screen);*/

	// Game engine
	GameEngine* gameEngine=new GameEngine(srf_screen);

	// Input
	Input* input=new Input();

	// Tileset test
	setTilesetSpecs(gameEngine, nullTilesetSurface, TILESET_TEST);

	// Build terrain
	gameEngine->addTerrainPartInstance(128,256,0);
	gameEngine->addTerrainPartInstance(128+32,256,0);
	gameEngine->addTerrainPartInstance(128+64,256,1);
	gameEngine->addTerrainPartInstance(128+96,256+16,2);

	// Add characters
	gameEngine->addCharacterInstance(CHARACTER_MEGAMAN, 120, 60, 1);

	// Set character surface
	gameEngine->characterInstances[0]->currentImage=srf_characterMegamanStandNormal;

	// Set character bounding box
	BoundingBox* tempBBox=&gameEngine->characterInstances[0]->boundingBox;
	tempBBox->rectEnabled=true;
	tempBBox->rectLeft=12;
	tempBBox->rectTop=4;
	tempBBox->rectWidth=16;
	tempBBox->rectHeight=32;

	// Last frame
	u32 lastTicks=SDL_GetTicks();

	// Loop
	while (true)
	{
		// Handle input
			// Scan keys
			input->scanKeys();

			// Right
			if (input->keysHeld&KEY_RIGHT)
			{
				gameEngine->characterInstances[0]->setActions(C_ACTION_RIGHT);
			}

			// Left
			if (input->keysHeld&KEY_LEFT)
			{
				gameEngine->characterInstances[0]->setActions(C_ACTION_LEFT);
			}

			// Fire
			if (input->keysPressed&KEY_FIRE)
			{
				gameEngine->characterInstances[0]->setActions(C_ACTION_FIRE);
			}

			// Slide
			if (input->keysHeld&KEY_SLIDE)
			{
				gameEngine->characterInstances[0]->setActions(C_ACTION_SLIDE);
			}

			// Jump
			if (input->keysHeld&KEY_JUMP)
			{
				gameEngine->characterInstances[0]->setActions(C_ACTION_JUMP);
			}

			// Escape
			if (input->keysPressed&KEY_ESCAPE)
			{
				break;
			}
		// Iterate
		gameEngine->iterateCharacters();

		// Draw
			// Clear screen
			SDL_FillRect(srf_screen, NULL, 0xFF00007F);

			// Game engine
			gameEngine->applyGraphics();

		// Update screen (aka double buffering)
		SDL_Flip(srf_screen);

		// Delay so we keep the fps
		while (SDL_GetTicks()-lastTicks<1000/GAME_FPS)
		{
			SDL_Delay(10);
		}
		lastTicks=SDL_GetTicks();
	}

	// Clean up
		// Null tileset surface
		free(nullTilesetSurface);

		// Game engine
		delete gameEngine;

		// Input
		delete input;

	// Quit
	SDL_Quit();

	// Return
	return 0;
}
